<template>
    <div>
        <el-dialog
            :title="!form.id ? '新增' : '修改'"
            :visible.sync="visible"
            :close-on-click-modal="false">

            <div class="table_form_wrap">
                <div class="left">
                    <el-form ref="form" :model="form" :rules="rules" @keyup.enter.native="handleSubmit()" label-width="120px">
                        <el-form-item label="用户名" prop="username">
                            <el-input :disabled="form.id && form.username==='admin'" placeholder="" v-model="form.username" clearable></el-input>
                        </el-form-item>
                        <el-form-item label="姓名" prop="realname">
                            <el-input placeholder="" v-model="form.realname" clearable></el-input>
                        </el-form-item>
                        <el-form-item label="性别" prop="sex">
                            <el-select v-model="form.sex" placeholder="请选择" style="width: 100%">
                                <el-option label="男" :value="1"></el-option>
                                <el-option label="女" :value="2"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="手机号码" prop="mobile">
                            <el-input placeholder="" v-model="form.mobile" clearable></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱地址" prop="email">
                            <el-input placeholder="" v-model="form.email" clearable></el-input>
                        </el-form-item>
                        <el-form-item label="分配角色" prop="roleIds">
                            <el-select
                                v-model="form.roleIds"
                                multiple
                                filterable
                                allow-create
                                default-first-option
                                placeholder="请选择角色"
                                style="width: 100%">
                                <el-option
                                    v-for="item in roles"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>

                        <el-form-item label="关联员工" prop="empName">
                            <el-input placeholder="请选择..." :disabled="true" v-model="form.empName" class="">
                                <el-button @click="handleEmpDeptDialogVisible" slot="append" icon="el-icon-search" class="choose_emp_btn">关联员工</el-button>
                                <el-button @click="handleEmpDeptClear" slot="append" icon="el-icon-close" class="del_emp_btn"></el-button>
                            </el-input>
                        </el-form-item>

                        <el-form-item label="状态" prop="status">
                            <el-radio-group v-model="form.status">
                                <el-radio-button :label="0">正常</el-radio-button>
                                <el-radio-button :label="1">锁定</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="right">
                    <LocalSingleUpload v-model="form.avatar" listType="picture-card"></LocalSingleUpload>
                    <i style="color: red" class="mt10">尺寸：150x200</i>
                </div>
            </div>


            <div slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button @click="handleSubmit()" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>

        </el-dialog>

        <!-- 员工查询窗口 -->
        <EmpDeptDialog ref="EmpDeptDialog" :multiple="false" @data="handleChooseEmps"></EmpDeptDialog>

    </div>
</template>

<script>
import LocalSingleUpload from '@/views/components/upload/local-single-upload'
import EmpDeptDialog from '@/views/hr/employee/components/EmpDeptDialog'
export default {
    components: {
        LocalSingleUpload,
        EmpDeptDialog
    },
    data() {
        return {
            visible: false,
            submitLoading: false,

            form: {
                empId: null,
                empName: null,
                realname: null,
                username: null,
                password: null,
                salt: null,
                mobile: null,
                email: null,
                avatar: null,
                sex: null,
                status: 1,
                roleIds: []
            },
            rules: {
                realname: [
                    { required: true, message: '姓名不能为空', trigger: 'blur' }
                ],
                username: [
                    { required: true, message: '用户账号不能为空', trigger: 'blur' }
                ],
                sex: [
                    { required: true, message: '性别不能为空', trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '手机号码不能为空', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '邮箱不能为空', trigger: 'blur' }
                ]
            },
            roles: []
        }
    },

    methods: {
        init(id) {
            const refs = this.$refs
            this.form.id = id || null
            this.form.avatar = null
            this.visible = true

            this.$nextTick(() => {
                refs['form'].resetFields()
                this.getRoles()
                if(this.form.id) {
                    this.getInfo(this.form.id)
                }
            })
        },

        /**
         * 员工查询窗口
         */
        handleEmpDeptDialogVisible() {
            // 表单窗口
            this.$nextTick(() => {
                this.$refs.EmpDeptDialog.show()
            })
        },


        /**
         * 详细信息
         */
        async getInfo(id) {
            if (id) {
                const res = await this.$http({
                    url: '/system/user/info/' + id,
                    method: 'GET'
                })
                this.form = Object.assign({}, res.data)
            }
        },

        /**
         * 提交表单
         */
        async handleSubmit() {
            const refs = this.$refs
            this.submitLoading = true
            const valid = await refs['form'].validate().catch(() => {})
            if(valid) {
                const res = await this.$http({
                    url: `/system/user/${!this.form.id ? 'save' : 'update'}`,
                    method: 'POST',
                    data: this.form
                })
                if (this.ResultMessage(res)) {
                    this.$emit('refreshDataList')
                    this.submitLoading = false
                    this.visible = false
                } else {
                    this.submitLoading = false
                }
            } else {
                this.submitLoading = false
            }
        },

        /**
         * 获取角色列表
         */
        async getRoles() {
            const res = await this.$http({
                url: '/system/role/list',
                method: 'GET'
            })
            this.roles = res.data
        },

        /**
         * 设置用户关联员工
         * @param {*} data
         */
        handleChooseEmps(data) {
            this.form = Object.assign({}, this.form, {empId: data[0].id, empName: data[0].name})
        },

        /**
         * 清空选着
         */
        handleEmpDeptClear() {
            this.form.empId = null
            this.form.empName = null
        },

    }
};
</script>

<style lang="scss" scoped>
.table_form_wrap{
    display: flex;
    .left {
        flex: 1;
    }
    .right{
        width: 300px;
        margin-left: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;
        padding-top: 20px;
    }
    .el-input-group__append{
        .choose_emp_btn{
            color: #409EFF;
        }
        .del_emp_btn{
            color: red;
            border-top-left-radius: 0;
            border-bottom-left-radius: 0;
            border-left: 1px solid #DCDFE6;
        }
    }
}
</style>
